﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ePrice.Models;

namespace ePrice.Controllers
{
    public class ProductController : Controller
    {
        EpriceEntities db = new EpriceEntities();
        //
        // GET: /Product/

        public ActionResult Index()
        {
            return RedirectToAction("Category");
        }

        // GET: /Product/Category/?

        public ActionResult Category(int? id)
        {
            // if category id is null
            if (!id.HasValue)
            {
                ViewBag.ListMode = true;
                var categories = db.Categories.ToList();
                ViewBag.List = categories;
                return View();
            }
            else
            {
                ViewBag.ListMode = false;
                var category = db.Categories.Include("Products").SingleOrDefault(c => c.CategoryId == id.Value);
                if (category != null)
                {
                    return View(category);
                }
                else
                {
                    //TODO: If product id is not valid
                    return View();
                }
            }
        }

        // POST:
        [HttpPost]
        public ActionResult Category(int? id, FormCollection fc)
        {
            return Category(id);
        }

        // GET: /Product/Details/?

        public ActionResult Details(int? id)
        {
            if (!id.HasValue)
            {
                ViewBag.ListMode = true;
                var products = db.Products.ToList();
                ViewBag.List = products;
                return View();
            }
            else
            {
                ViewBag.ListMode = false;
                var product = db.Products.Find(id);
                if (product != null)
                {
                    return View(product);
                }
                else
                    //TODO: If product id is not valid
                    return View();

            }
        }

        //POST:
        [HttpPost]
        public ActionResult Details(int? id, FormCollection fc)
        {
            return Details(id);
        }

        [HandleError(ExceptionType=typeof(Exception),
            Master="abc",
            Order=1,
            View="ada")]
        [ChildActionOnly]
        public ActionResult CommentPartial(int id)
        {
            try
            {
                var comments = db.Comments.ToList();
                return PartialView(comments);
            }
            catch
            {
                return Redirect("http://gooogle.com.vn");
            }
        }
    }
}
